package com;

import java.util.concurrent.*;

public class h8 {
    public static void main(String[] args) throws Exception {
        int repeat = 2;
        Exchanger<String> exc = new Exchanger<>();
        new Thread(() -> {
            for (int y = 0; y < repeat; y++) {
                String info = null;
                if (y % 2 == 0) {
                    info = "李兴华高薪就业编程训练营:edu.yootk.com";
                } else {
                    info = "沐言科技:www.yootk.com";
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    exc.exchange(info);
                    System.out.println("【" + Thread.currentThread().getName() + "】" + info);
                } catch (InterruptedException e) {
                }
            }
        }, "信息生产者").start();
        new Thread(()->{
            for (int y=0;y<repeat;y++){
                try{
                    TimeUnit.SECONDS.sleep(1);
                    String info= exc.exchange(null);
                    System.out.println("【"+Thread.currentThread().getName()+"】"+ info);
                }catch (InterruptedException e){}
            }
        },"信息消费者").start();
    }
}
